﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BT_DoHoa_Nhom13.Lib;
using BT_DoHoa_Nhom13.ShapeLib;
using BT_DoHoa_Nhom13.GraphicLib;
using BT_DoHoa_Nhom13.DiagramFactorys;
using BT_DoHoa_Nhom13.EffectLib;
using System.IO;
namespace BT_DoHoa_Nhom13
{
    public partial class Form1 : Form
    {
        MyPanel panel;
        List<RadioButton> normalShapes;
        List<RadioButton> diagramShapes;
        Boolean isCanDraw;

        public Form1()
        {
            InitializeComponent();
            panel = new MyPanel(panel1);
            isCanDraw = false;
        }

        private void Graphic_Checked(object sender, EventArgs e)
        {
            RadioButton btt= (RadioButton)sender;
            if(btt.Checked)
            {
                panel.SetCurrentGraphic(btt.Text, panel1);
            }

            panel1.Refresh();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if(!isCanDraw)
            {
                panel.MouseDown(e.X, e.Y);
                isCanDraw = true;
            }
           
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if(isCanDraw)
            {
                panel.MouseUp(e.X, e.Y);
                panel1.Refresh();
                isCanDraw = false;
            }
            
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if(isCanDraw)
            {
                panel.MouseMove(e.X, e.Y);
                panel1.Refresh();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel.Clear();
            panel1.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Image Files (*.png)|*.png";
            if(sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                panel.Save(sfd.FileName, panel1.Width, panel1.Height);
                MessageBox.Show("Hình đã được Save tại " + sfd.FileName);
            }
            
        }

        private void FactoryChanged(object sender, EventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            panel.SetCurrentFactory(cb.SelectedItem.ToString());
            comboBox2.Items.Clear();
            comboBox2.Items.AddRange(panel.GetAvailableShapes().ToArray());
            comboBox2.SelectedIndex = 0;
            panel1.Refresh();
        }


        private void ShapeChanged(object sender, EventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            panel.SetCurrentShape(cb.SelectedItem.ToString());
        }

        private void Effect_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            if (cb.Checked)
            {
                panel.AddEffect(cb.Text);
            }
            else
            {
                panel.RemoveEffect(cb.Text);
            }

            panel1.Refresh();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<String> nameOfFactorys = panel.GetListNameOfFactory();

            comboBox1.Items.Add("None Diagram");
            foreach (String factory in nameOfFactorys)
            {
                comboBox1.Items.Add(factory);
            }

            List<RadioButton> lstGraphicButtons = new List<RadioButton>();
            lstGraphicButtons.Add(radioButton1);
            lstGraphicButtons.Add(radioButton2);

            List<String> nameOfGraphics = panel.GetNameOfGraphics();
            if(lstGraphicButtons.Count != nameOfGraphics.Count)
            {
                MessageBox.Show("Kiem tra sl graphic va radiobutton");
                return;
            }

            for(int i=0;i< lstGraphicButtons.Count; i++)
            {
                lstGraphicButtons[i].Text = nameOfGraphics[i];
            }

            comboBox1.SelectedIndex = 0;
            radioButton2.Checked = true;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            panel.Draw(e.Graphics);
        }

      
    }
}
